//
//  PhotoViewController.m
//  XhlTrip
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "PhotoViewController.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"

@interface PhotoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollerView;
@property (nonatomic, assign) NSInteger buttontag;

@end

@implementation PhotoViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.phototArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createScroller];

    self.view.backgroundColor = [UIColor blackColor];
}

- (void)createScroller
{
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 60 * Height, self.view.frame.size.width, self.view.frame.size.height - 6 * Height)];
    self.scrollerView.backgroundColor = [UIColor blackColor];
    
    self.scrollerView.pagingEnabled = YES;
    for (int i = 0; i < [self.phototArr count]; i++) {
        if (![[_phototArr[i] objectForKey:@"photo_url"] isKindOfClass:[NSNull class]]) {
            CGFloat photoWith = [[self class] heightWithImage:self.phototArr[i] weidht:self.view.frame.size.width];
            UIButton *photoButton = [UIButton buttonWithType:UIButtonTypeSystem];
            NSString *urlStr =[self.phototArr[i] objectForKey:@"photo_url"];
            
            photoButton.frame = CGRectMake(i * self.view.frame.size.width, 30 * Height, self.view.frame.size.width, photoWith);
            
            [photoButton sd_setBackgroundImageWithURL:[NSURL URLWithString:urlStr] forState:UIControlStateNormal];

            photoButton.tag = i + 1;
            [photoButton addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollerView addSubview:photoButton];
        }
      
    }
   
    self.scrollerView.contentSize = CGSizeMake(self.view.frame.size.width * self.phototArr.count, 0);
    self.scrollerView.contentOffset = CGPointMake(self.view.frame.size.width * _h, 0);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setBackgroundImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    button.frame = CGRectMake(10 * Width, 25 * Height, 35 * Width, 35 * Height);
    [button addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.scrollerView];
    [_scrollerView release];
}

- (void)backButton:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark - 图片高度自适应
+ (CGFloat)heightWithImage:(NSDictionary *)magedic weidht:(CGFloat)weigth
{
   
   CGFloat newh = [[magedic objectForKey:@"height"] floatValue] * weigth / [[magedic objectForKey:@"width"] floatValue];
    /* 返回高度 */
    return newh;
    
}

#pragma mark - 保存图片到本地
- (void)save:(UIButton *)button
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存到手机" message:@"是否保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    self.buttontag = button.tag;
    [alert show];
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
       
        UIImageView *itmage = [[UIImageView alloc] init];
        [itmage sd_setImageWithURL:[self.phototArr[self.buttontag - 1] objectForKey:@"photo_url"]];
        UIImageWriteToSavedPhotosAlbum(itmage.image, nil, nil, nil);
    }
}

- (void)dealloc
{
    [_scrollerView release];
    [_phototArr release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
